var app = getApp()
var id;
var name;
var price ;

const date = new Date()
const years = []
const months = []
const days = []


for (let i = 1990; i <= date.getFullYear(); i++) {
  years.push(i)
}

for (let i = 1; i <= 12; i++) {
  months.push(i)
}

for (let i = 1; i <= 31; i++) {
  days.push(i)
}
Page({
  data:{
    //数组中保存的可选日期
    years: years,
    months: months,
    days: days,
    //默认的顶部日期
    year: date.getFullYear(),
    month: date.getMonth() + 1,
    day: date.getDate(),
    //滑动框中放入的是第几个值
    value: [date.getFullYear(), date.getMonth(), date.getDate() - 1],
    date: "09-01",
    current: 0,
  },
  onPullDownRefresh: function () {
    console.log('onPullDownRefresh')
  },
  onLoad:function(options){
    // 页面初始化 options为页面跳转所带来的参数
    var that = this;
    var cid = options.cid;
    wx.setStorageSync('cids', cid);
    var shopid = options.shopid;
    var uid = options.uid;
    wx.request({
      url: 'https://www.cakeyouxuan.com/small/shop/jiesuan',
      data: {
        carid: cid,
        uid: uid,
        shopid: shopid,
      },
      method: "POST",
      header: {
        "Content-Type": "application/x-www-form-urlencoded"  //post
      },
      success: function (res) {
        that.setData({
          cakes: res.data.cakes,
          present: res.data.present,
          store: res.data.store,
          sums: res.data.sums,
          psf: res.data.psf,
          ziti_pay: res.data.ziti_pay,
        });
      },
    })
  },
  //详情页跳转
  lookdetail:function(e){
    var lookid=e.currentTarget.dataset;
    console.log(e.currentTarget.dataset.id);
    wx.navigateTo({
      url:"/pages/yiguo/detail/detail?id="+lookid.id
    })
  },
  switchSlider:function(e){
    wx.setStorageSync('delivery_type', e.target.dataset.index);
    this.setData({
      current:e.target.dataset.index
    })
  },
  changeSlider:function(e){
    this.setData({
      current: e.detail.current
    })
  },
  bindDateChange: function (e) {
    this.setData({
      date: e.detail.value
    })
  },
  bindChange: function (e) {
    const val = e.detail.value
    this.setData({
      year: this.data.years[val[0]],
      month: this.data.months[val[1]],
      day: this.data.days[val[2]]
    })
  },

  //拨打电话
  callphone: function(e) {
      var phones = '18205214377';
      wx.makePhoneCall({
        phoneNumber: phones 
      })
      console.log(phones);
  },

  skip: function (e) {
    wx.navigateTo({
      url: "/pages/yiguo/address2/address2"
    })
  },
  skip2: function (e) {
    wx.navigateTo({
      url: "/pages/yiguo/address/address"
    })
  },
  onReady:function(){
    // 页面渲染完成
  },
  onShow:function(){
    // 页面显示
  },
  onHide:function(){
    // 页面隐藏
  },
  onUnload:function(){
    // 页面关闭
  },

  formSubmit: function (e) {
    var username = e.detail.value.username;  //收货人
    var phone = e.detail.value.phone;  // 收货电话
    var message = e.detail.value.message; //备注
    var openid = app.whereId.openid; //openid
    var price = e.detail.target.dataset.id;  //价格  含配送费
    
    var psf = e.detail.value.psf;
    var year = e.detail.value.year;   //年
    var month = e.detail.value.month; //月
    var day = e.detail.value.day;     //日
    var address = e.detail.value.address;  //详细地址
    var sheng = e.detail.value.sheng;   //省
    var shi = e.detail.value.shi;    //市
    var area = e.detail.value.area;  //区
    var peis_time = e.detail.value.peis_time;  //配送时段
    var uid = app.whereId.uid;
    var shopid = app.whereId.shopid;
    var cid = wx.getStorageSync('cids');
    var delivery_type = wx.getStorageSync('delivery_type');
   console.log(openid+'第一个');
    if (delivery_type == "") {
      delivery_type = 1;
    } else {
      delivery_type = (delivery_type * 1) + 1;
    }
    
    if (address == undefined){
      wx.showToast({
        title: '请填写收货地址',
        icon: 'success',
        duration: 800
      })
    }else{
      wx.request({
        url: 'https://www.cakeyouxuan.com/small/payskpz/index',
        header: {
          "Content-Type": "application/x-www-form-urlencoded"
        },
        method: "POST",
        data: {
          openid: openid,
          price: price,
          psf: psf,
          username: username,
          phone: phone,
          message: message,
          shopid: shopid,
          uid: uid,
          address: address,
          dates: year + "-" + month + "-" + day,
          delivery_type: delivery_type,
          sheng: sheng,
          shi: shi,
          area: area,
          cid: cid,
          peis_time: peis_time,
        },
        success: function (res) {
          if (res.data.state == 1) {
            // --------- 订单生成成功，发起支付请求 ------------------
            wx.requestPayment({
              timeStamp: res.data.timeStamp,
              nonceStr: res.data.nonceStr,   //字符串随机数
              package: res.data.package,
              signType: res.data.signType,
              paySign: res.data.paySign,
              'success': function (res) {
                //requestPayment:ok==>调用支付成功
                if (res.errMsg == "requestPayment:ok") {
                  wx.request({
                    url: 'https://www.cakeyouxuan.com/small/payskpz/notifys',
                    data: {
                      uid: uid,
                    },
                    method: "POST",
                    header: {
                      "Content-Type": "application/x-www-form-urlencoded"  //post
                    },
                    success: function (res) {
                      wx.navigateTo({
                        url: "/pages/yiguo/success/success?price=" + price + "&id=" + res.data.ids,
                      })
                    },
                  })
                }
              },
              'fail': function (res) {
                wx.switchTab({
                  url: '/pages/yiguo/mine/mine',
                })
              },
              'complete': function (res) {
                
              }
            })
          } else if (res.data.state == 0) {
            wx.showToast({
              title: res.data.Msg,
              icon: 'fail',
              duration: 1000
            })
          } else {
            wx.showToast({
              title: '系统繁忙，请稍后重试~',
              icon: 'fail',
              duration: 1000
            })
          }
        }

      })
    }
    
  },

  formSubmit1: function (e) {
    var username = e.detail.value.name;  //收货人
    var phone = e.detail.value.dianhua;  // 收货电话
    var message = e.detail.value.messages; //备注
    // var openid = app.globalData.openid; //openid
    var openid = app.whereId.openid; //openid
    var price = e.detail.target.dataset.id;  //价格  含配送费
    var psf = '0.00';
    var year = e.detail.value.years;   //年
    var month = e.detail.value.yue; //月
    var day = e.detail.value.days;     //日
    var address = "";  //详细地址
    var sheng = "";   //省
    var shi = "";    //市
    var area = "";  //区
    var peis_time = e.detail.value.ps_time;  //配送时段
    var uid = app.whereId.uid;
    var shopid = app.whereId.shopid;
    console.log(openid);
    var cid = wx.getStorageSync('cids');
    var delivery_type = wx.getStorageSync('delivery_type');
    if (delivery_type == "") {
      delivery_type = 1;
    } else {
      delivery_type = (delivery_type * 1) + 1;
    }
    wx.request({
      url: 'https://www.cakeyouxuan.com/small/payskpz/index',
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      method: "POST",
      data: {
        openid: openid,
        price: price,
        psf: psf,
        username: username,
        phone: phone,
        message: message,
        shopid: shopid,
        uid: uid,
        address: address,
        dates: year + "-" + month + "-" + day,
        delivery_type: delivery_type,
        sheng: sheng,
        shi: shi,
        area: area,
        cid: cid,
        peis_time: peis_time,
      },
      success: function (res) {
        if (res.data.state == 1) {
          // --------- 订单生成成功，发起支付请求 ------------------
          wx.requestPayment({
            timeStamp: res.data.timeStamp,
            nonceStr: res.data.nonceStr,   //字符串随机数
            package: res.data.package,
            signType: res.data.signType,
            paySign: res.data.paySign,
            'success': function (res) {
              //requestPayment:ok==>调用支付成功
              if (res.errMsg == "requestPayment:ok") {
                wx.request({
                  url: 'https://www.cakeyouxuan.com/small/payskpz/notifys',
                  data: {
                    uid: uid,
                  },
                  method: "POST",
                  header: {
                    "Content-Type": "application/x-www-form-urlencoded"  //post
                  },
                  success: function (res) {
                    console.log(res)
                    wx.navigateTo({
                      url: "/pages/yiguo/success/success?price=" + res.data.price + "&id=" + res.data.ids,
                    })
                  },
                })
              }
            },
            'fail': function (res) {
              wx.switchTab({
                url: '/pages/yiguo/mine/mine',
              })
            },
            'complete': function (res) {
            }
          })
        } else if (res.data.state == 0) {
          wx.showToast({
            title: res.data.Msg,
            icon: 'fail',
            duration: 1000
          })
        } else {
          wx.showToast({
            title: '系统繁忙，请稍后重试~',
            icon: 'fail',
            duration: 1000
          })
        }
      }

    })
  },


})

